<?

if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
*  Controller公共基类
*  2012-9-24 by ZhouHuan
*/
class Base extends CI_Controller {
	
	protected function echo_error($msg) {
		show_error($msg, 403);
		$this->log_error($msg);
	}

	protected function log_error($msg) {
		log_message('error', $msg);
	}
    
    protected function log_trace($msg) {
        log_message('trace', $msg);
    }    

	protected function log_info($msg) {
		log_message('info',  $msg);
	}
    
    protected function dt() {
        return date("Y-m-d H:i:s"); 
    }

    /**
    * 判断用户是否已登录
    */
    protected function user_is_logined() {
        if(empty($_SESSION['user_id']))  {
            return false;
        }
        return true;  
    } 
    
    /**
    * 未登录用户直接拒绝
    */
    protected function check_user_logined() {
        if( ! $this->user_is_logined() )  {
            show_error('access denied');
        }  
    } 

    /**
    * 记录用户操作日志
    */
	protected function write_operate_log($msg) {
		$user_id = -1;
		if(isset($_SESSION['user_id'])) {
			$user_id = $_SESSION['user_id'];
		}
		$sql = "insert into visa_user_log(user_id, op_desc, request_uri, page_url, ip_address) 
		values(".$user_id.", '". $msg ."', '". $_SERVER['REQUEST_URI'] ."', '". $_SERVER['HTTP_REFERER'] ."', '". $_SERVER['REMOTE_ADDR'] ."')";
		$this->db->query($sql);
	}
    
    /**
    * 判断是否有sql非法关键词注入
    */
    protected function has_sql_inject($uid, $pwd)  {
        $patterns = array("'"," or "," union ", "update ", "delete ", "insert ");  
        foreach($patterns as $k => $v) {
            if(eregi($v, $uid) || eregi($v, $pwd)){ 
                return true; 
            } 
        }
        return false;
    } 
}
?>